package cn.fulong.smebj.admin.service.impl;

import cn.fulong.frame.config.Result;
import cn.fulong.smebj.admin.service.QuestionService;
import cn.fulong.smebj.entity.Question;
import cn.fulong.smebj.mapper.QuestionMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public Result<Object> questionList(Integer pageNo, Integer pageSize) {
        IPage<Map<String,Object>> iPage = new Page<>(Optional.ofNullable(pageNo).orElse(0), pageSize);
        IPage<Map<String,Object>> questionList = questionMapper.selectAll(iPage);
        return Result.success(questionList);
    }

    @Override
    public Result<Object> questionAnswerList(Integer pageNo, Integer pageSize) {
        IPage<Map<String,Object>> iPage = new Page<>(Optional.ofNullable(pageNo).orElse(0), pageSize);
        IPage<Map<String,Object>> questionList = questionMapper.selectAlreadyAnswer(iPage);
        return Result.success(questionList);
    }


    @Override
    public Result<Object> addQuestion(Question question) {
        try {
            question.setCreateTime(LocalDateTime.now());
            questionMapper.insert(question);
            return Result.success();
        }catch (Exception e){
            return Result.failure(e.getMessage());
        }
    }

    @Override
    public Result<Object> editQuestion(Question question) {
        try {
            questionMapper.updateById(question);
            return Result.success();
        }catch (Exception e){
            return Result.failure(e.getMessage());
        }
    }

    @Override
    public Result<Object> deleteQuestion(Long id) {
        questionMapper.deleteById(id);
        return Result.success();
    }

}
